package com.zw.yzk.kabu.presenter;

import android.support.v7.app.AppCompatActivity;

import com.zw.yzk.kabu.R;
import com.zw.yzk.kabu.base.BaseSubscriber;
import com.zw.yzk.kabu.data.network.entity.AlbumEntity;
import com.zw.yzk.kabu.data.network.task.AlbumTask;
import com.zw.yzk.kabu.domain.repository.UseCase;
import com.zw.yzk.kabu.management.LoadingDialogManager;
import com.zw.yzk.kabu.mapper.AlbumMapper;
import com.zw.yzk.kabu.view.view.AlbumView;

import javax.inject.Inject;

/**
 * Created by zhanwei on 2017/7/5.
 */

public class AlbumPresenter implements Presenter {

    private UseCase getAlbum;
    private AppCompatActivity activity;
    private AlbumView view;
    private AlbumMapper mapper;

    @Inject
    public AlbumPresenter(UseCase getAlbum, AppCompatActivity activity, AlbumMapper mapper) {
        this.getAlbum = getAlbum;
        this.activity = activity;
        this.mapper = mapper;
    }

    public void setView(AlbumView view) {
        this.view = view;
    }

    public void getAlbum(int userId, int lastId) {
        getAlbum.execute(new AlbumSubscriber(activity), new AlbumTask(userId, lastId));
    }

    @Override
    public void resume() {

    }

    @Override
    public void pause() {

    }

    @Override
    public void destroy() {
        getAlbum.unSubscribe();
    }

    private class AlbumSubscriber extends BaseSubscriber<AlbumEntity> {

        public AlbumSubscriber(AppCompatActivity appCompatActivity) {
            super(appCompatActivity);
        }

        @Override
        public void onNext(AlbumEntity albumEntity) {
            super.onNext(albumEntity);
            view.getAlbumSuccess(mapper.transfer(albumEntity));
        }

        @Override
        public void onError(Throwable e) {
            super.onError(e);
            view.getAlbumFailed();
        }

        @Override
        public void onCompleted() {
            super.onCompleted();
            LoadingDialogManager.getInstance().dismissLoadingDialog();
        }

        @Override
        public String addErrorMessage() {
            return activity.getResources().getString(R.string.get_album_failed);
        }
    }
}
